import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

class Message{
    public String from;
    public String to;
    public String message;

    public Message(String from, String to, String message) {
        this.from = from;
        this.to = to;
        this.message = message;
    }
}
@WebServlet("/messageWall")
public class MessageWall extends HttpServlet {
    List<Message> list = new ArrayList<>();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        WebContext context = new WebContext(req,resp,this.getServletContext());
        context.setVariable("messages",list);
        TemplateEngine engine = (TemplateEngine) this.getServletContext().getAttribute("engine");
        engine.process("message",context,resp.getWriter());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String from = req.getParameter("from");
        String to = req.getParameter("to");
        String message = req.getParameter("message");
        list.add(new Message(from,to,message));
        resp.sendRedirect("messageWall");
    }
}
